﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Reflection
Imports System.Text

Namespace System.Workflow.ComponentModel.Compiler
    Friend NotInheritable Class RTTypeWrapper
        Inherits Type
        Implements ICloneable
        ' Methods
        Friend Sub New(ByVal typeProvider As ITypeProvider, ByVal runtimeType As Type)
            Me.memberMapping = New Hashtable
            Me.boundedTypes = New Hashtable(New TypeArrayComparer)
            If (runtimeType Is Nothing) Then
                Throw New ArgumentNullException("runtimeType")
            End If
            If (runtimeType.Assembly Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_InvalidRuntimeType"), "runtimeType")
            End If
            Me.typeProvider = typeProvider
            Me.runtimeType = runtimeType
        End Sub

        Private Sub New(ByVal typeProvider As ITypeProvider, ByVal runtimeType As Type, ByVal typeArgs As Type())
            Me.memberMapping = New Hashtable
            Me.boundedTypes = New Hashtable(New TypeArrayComparer)
            If (runtimeType Is Nothing) Then
                Throw New ArgumentNullException("runtimeType")
            End If
            If (runtimeType.Assembly Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "runtimeType")
            End If
            Me.typeProvider = typeProvider
            Me.runtimeType = runtimeType
            If Not Me.IsGenericTypeDefinition Then
                Throw New ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "runtimeType")
            End If
            Me.typeArgs = New Type(typeArgs.Length  - 1) {}
            Dim i As Integer
            For i = 0 To typeArgs.Length - 1
                Me.typeArgs(i) = typeArgs(i)
                If (Me.typeArgs(i) Is Nothing) Then
                    Throw New ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "typeArgs")
                End If
            Next i
        End Sub

        Public Function Clone() As Object
            Return Me
        End Function

        Private Function EnsureConstructorWrapped(ByVal realInfo As ConstructorInfo) As ConstructorInfo
            Dim info As ConstructorInfo = DirectCast(Me.memberMapping.Item(realInfo), ConstructorInfo)
            If (info Is Nothing) Then
                info = New RTConstructorInfoWrapper(Me, realInfo)
                Me.memberMapping.Add(realInfo, info)
            End If
            Return info
        End Function

        Private Function EnsureEventWrapped(ByVal realInfo As EventInfo) As EventInfo
            Dim info As EventInfo = DirectCast(Me.memberMapping.Item(realInfo), EventInfo)
            If (info Is Nothing) Then
                info = New RTEventInfoWrapper(Me, realInfo)
                Me.memberMapping.Add(realInfo, info)
            End If
            Return info
        End Function

        Private Function EnsureFieldWrapped(ByVal realInfo As FieldInfo) As FieldInfo
            Dim info As FieldInfo = DirectCast(Me.memberMapping.Item(realInfo), FieldInfo)
            If (info Is Nothing) Then
                info = New RTFieldInfoWrapper(Me, realInfo)
                Me.memberMapping.Add(realInfo, info)
            End If
            Return info
        End Function

        Private Function EnsureMemberWrapped(ByVal memberInfo As MemberInfo) As MemberInfo
            Dim info As MemberInfo = Nothing
            If TypeOf memberInfo Is PropertyInfo Then
                Return Me.EnsurePropertyWrapped([TryCast](memberInfo,PropertyInfo))
            End If
            If TypeOf memberInfo Is ConstructorInfo Then
                Return Me.EnsureConstructorWrapped([TryCast](memberInfo,ConstructorInfo))
            End If
            If TypeOf memberInfo Is EventInfo Then
                Return Me.EnsureEventWrapped([TryCast](memberInfo,EventInfo))
            End If
            If TypeOf memberInfo Is FieldInfo Then
                Return Me.EnsureFieldWrapped([TryCast](memberInfo,FieldInfo))
            End If
            If TypeOf memberInfo Is MethodInfo Then
                info = Me.EnsureMethodWrapped([TryCast](memberInfo,MethodInfo))
            End If
            Return info
        End Function

        Friend Function EnsureMethodWrapped(ByVal realInfo As MethodInfo) As MethodInfo
            Dim info As MethodInfo = DirectCast(Me.memberMapping.Item(realInfo), MethodInfo)
            If (info Is Nothing) Then
                info = New RTMethodInfoWrapper(Me, realInfo)
                Me.memberMapping.Add(realInfo, info)
            End If
            Return info
        End Function

        Private Function EnsurePropertyWrapped(ByVal realInfo As PropertyInfo) As PropertyInfo
            Dim info As PropertyInfo = DirectCast(Me.memberMapping.Item(realInfo), PropertyInfo)
            If (info Is Nothing) Then
                info = New RTPropertyInfoWrapper(Me, realInfo)
                Me.memberMapping.Add(realInfo, info)
            End If
            Return info
        End Function

        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            Dim runtimeType As Type = [TryCast](obj,Type)
            If TypeOf runtimeType Is RTTypeWrapper Then
                runtimeType = DirectCast(runtimeType, RTTypeWrapper).runtimeType
            End If
            Return (Me.runtimeType Is runtimeType)
        End Function

        Public Overrides Function GetArrayRank() As Integer
            Return Me.runtimeType.GetArrayRank
        End Function

        Protected Overrides Function GetAttributeFlagsImpl() As TypeAttributes
            Return Me.runtimeType.Attributes
        End Function

        Protected Overrides Function GetConstructorImpl(ByVal bindingAttr As BindingFlags, ByVal binder As Binder, ByVal callConvention As CallingConventions, ByVal types As Type(), ByVal modifiers As ParameterModifier()) As ConstructorInfo
            Dim info As ConstructorInfo
            For Each info In Me.runtimeType.GetConstructors(bindingAttr)
                Dim flag As Boolean = False
                If (Not types Is Nothing) Then
                    Dim parameters As ParameterInfo() = info.GetParameters
                    If (parameters.GetLength(0) = types.Length) Then
                        Dim i As Integer = 0
                        Do While (Not flag AndAlso (i < parameters.Length))
                            flag = Not Me.IsAssignable(parameters(i).ParameterType, types(i))
                            i += 1
                        Loop
                    Else
                        flag = True
                    End If
                End If
                If Not flag Then
                    Return Me.EnsureConstructorWrapped(info)
                End If
            Next
            Return Nothing
        End Function

        Public Overrides Function GetConstructors(ByVal bindingAttr As BindingFlags) As ConstructorInfo()
            Dim list As New List([Of] ConstructorInfo)
            Dim info As ConstructorInfo
            For Each info In Me.runtimeType.GetConstructors(bindingAttr)
                list.Add(Me.EnsureConstructorWrapped(info))
            Next
            Return list.ToArray
        End Function

        Public Overrides Function GetCustomAttributes(ByVal inherit As Boolean) As Object()
            Return Me.runtimeType.GetCustomAttributes(inherit)
        End Function

        Public Overrides Function GetCustomAttributes(ByVal attributeType As Type, ByVal inherit As Boolean) As Object()
            Return Me.runtimeType.GetCustomAttributes(attributeType, inherit)
        End Function

        Public Overrides Function GetElementType() As Type
            Return Me.ResolveTypeFromTypeSystem(Me.runtimeType.GetElementType)
        End Function

        Public Overrides Function GetEvent(ByVal name As String, ByVal bindingAttr As BindingFlags) As EventInfo
            Dim realInfo As EventInfo = Me.runtimeType.GetEvent(name, bindingAttr)
            If (Not realInfo Is Nothing) Then
                realInfo = Me.EnsureEventWrapped(realInfo)
            End If
            Return realInfo
        End Function

        Public Overrides Function GetEvents(ByVal bindingAttr As BindingFlags) As EventInfo()
            Dim list As New List([Of] EventInfo)
            Dim info As EventInfo
            For Each info In Me.runtimeType.GetEvents(bindingAttr)
                list.Add(Me.EnsureEventWrapped(info))
            Next
            Return list.ToArray
        End Function

        Public Overrides Function GetField(ByVal name As String, ByVal bindingAttr As BindingFlags) As FieldInfo
            Dim field As FieldInfo = Me.runtimeType.GetField(name, bindingAttr)
            If (Not field Is Nothing) Then
                field = Me.EnsureFieldWrapped(field)
            End If
            Return field
        End Function

        Public Overrides Function GetFields(ByVal bindingAttr As BindingFlags) As FieldInfo()
            Dim list As New List([Of] FieldInfo)
            Dim info As FieldInfo
            For Each info In Me.runtimeType.GetFields(bindingAttr)
                list.Add(Me.EnsureFieldWrapped(info))
            Next
            Return list.ToArray
        End Function

        Public Overrides Function GetGenericArguments() As Type()
            Return Me.typeArgs
        End Function

        Public Overrides Function GetGenericTypeDefinition() As Type
            If Me.IsGenericType Then
                Return Me.runtimeType
            End If
            Return Me
        End Function

        Public Overrides Function GetHashCode() As Integer
            Return Me.runtimeType.GetHashCode
        End Function

        Public Overrides Function GetInterface(ByVal name As String, ByVal ignoreCase As Boolean) As Type
            Dim type As Type = Me.runtimeType.GetInterface(name, ignoreCase)
            If (Not type Is Nothing) Then
                type = Me.ResolveTypeFromTypeSystem(type)
            End If
            Return type
        End Function

        Public Overrides Function GetInterfaces() As Type()
            Dim list As New List([Of] Type)
            Dim type As Type
            For Each type In Me.runtimeType.GetInterfaces
                Dim item As Type = Me.ResolveTypeFromTypeSystem(type)
                list.Add(item)
            Next
            Return list.ToArray
        End Function

        Public Overrides Function GetMember(ByVal name As String, ByVal type As MemberTypes, ByVal bindingAttr As BindingFlags) As MemberInfo()
            Dim list As New List([Of] MemberInfo)
            Dim info As MemberInfo
            For Each info In Me.runtimeType.GetMember(name, type, bindingAttr)
                list.Add(Me.EnsureMemberWrapped(info))
            Next
            Return list.ToArray
        End Function

        Public Overrides Function GetMembers(ByVal bindingAttr As BindingFlags) As MemberInfo()
            Dim list As New List([Of] MemberInfo)
            Dim info As MemberInfo
            For Each info In Me.runtimeType.GetMembers(bindingAttr)
                list.Add(Me.EnsureMemberWrapped(info))
            Next
            Return list.ToArray
        End Function

        Protected Overrides Function GetMethodImpl(ByVal name As String, ByVal bindingAttr As BindingFlags, ByVal binder As Binder, ByVal callConvention As CallingConventions, ByVal types As Type(), ByVal modifiers As ParameterModifier()) As MethodInfo
            Dim info As MethodInfo
            For Each info In Me.runtimeType.GetMethods(bindingAttr)
                If If(((bindingAttr And BindingFlags.IgnoreCase) = BindingFlags.IgnoreCase), (String.Compare(info.Name, name, StringComparison.OrdinalIgnoreCase) = 0), (String.Compare(info.Name, name, StringComparison.Ordinal) = 0)) Then
                    Dim flag2 As Boolean = False
                    If (Not types Is Nothing) Then
                        Dim parameters As ParameterInfo() = info.GetParameters
                        If (parameters.GetLength(0) = types.Length) Then
                            Dim i As Integer = 0
                            Do While (Not flag2 AndAlso (i < parameters.Length))
                                flag2 = Not Me.IsAssignable(parameters(i).ParameterType, types(i))
                                i += 1
                            Loop
                        Else
                            flag2 = True
                        End If
                    End If
                    If Not flag2 Then
                        Return Me.EnsureMethodWrapped(info)
                    End If
                End If
            Next
            Return Nothing
        End Function

        Public Overrides Function GetMethods(ByVal bindingAttr As BindingFlags) As MethodInfo()
            Dim list As New List([Of] MethodInfo)
            Dim info As MethodInfo
            For Each info In Me.runtimeType.GetMethods(bindingAttr)
                list.Add(Me.EnsureMethodWrapped(info))
            Next
            Return list.ToArray
        End Function

        Public Overrides Function GetNestedType(ByVal name As String, ByVal bindingAttr As BindingFlags) As Type
            Dim nestedType As Type = Me.runtimeType.GetNestedType(name, bindingAttr)
            If (Not nestedType Is Nothing) Then
                nestedType = Me.ResolveTypeFromTypeSystem(nestedType)
            End If
            Return nestedType
        End Function

        Public Overrides Function GetNestedTypes(ByVal bindingAttr As BindingFlags) As Type()
            Dim list As New List([Of] Type)
            Dim type As Type
            For Each type In Me.runtimeType.GetNestedTypes(bindingAttr)
                list.Add(Me.ResolveTypeFromTypeSystem(type))
            Next
            Return list.ToArray
        End Function

        Public Overrides Function GetProperties(ByVal bindingAttr As BindingFlags) As PropertyInfo()
            Dim list As New List([Of] PropertyInfo)
            Dim info As PropertyInfo
            For Each info In Me.runtimeType.GetProperties(bindingAttr)
                list.Add(Me.EnsurePropertyWrapped(info))
            Next
            Return list.ToArray
        End Function

        Protected Overrides Function GetPropertyImpl(ByVal name As String, ByVal bindingAttr As BindingFlags, ByVal binder As Binder, ByVal returnType As Type, ByVal types As Type(), ByVal modifiers As ParameterModifier()) As PropertyInfo
            Dim info As PropertyInfo
            For Each info In Me.runtimeType.GetProperties(bindingAttr)
                If (If(((bindingAttr And BindingFlags.IgnoreCase) = BindingFlags.IgnoreCase), (String.Compare(info.Name, name, StringComparison.OrdinalIgnoreCase) = 0), (String.Compare(info.Name, name, StringComparison.Ordinal) = 0)) AndAlso ((returnType Is Nothing) OrElse returnType.Equals(info.PropertyType))) Then
                    Dim flag2 As Boolean = False
                    If (Not types Is Nothing) Then
                        Dim indexParameters As ParameterInfo() = info.GetIndexParameters
                        If (indexParameters.GetLength(0) = types.Length) Then
                            Dim i As Integer = 0
                            Do While (Not flag2 AndAlso (i < indexParameters.Length))
                                flag2 = Not Me.IsAssignable(indexParameters(i).ParameterType, types(i))
                                i += 1
                            Loop
                        Else
                            flag2 = True
                        End If
                    End If
                    If Not flag2 Then
                        Return Me.EnsurePropertyWrapped(info)
                    End If
                End If
            Next
            Return Nothing
        End Function

        Protected Overrides Function HasElementTypeImpl() As Boolean
            Return Me.runtimeType.HasElementType
        End Function

        Public Overrides Function InvokeMember(ByVal name As String, ByVal bindingFlags As BindingFlags, ByVal binder As Binder, ByVal target As Object, ByVal providedArgs As Object(), ByVal modifiers As ParameterModifier(), ByVal culture As CultureInfo, ByVal namedParams As String()) As Object
            Return Me.runtimeType.InvokeMember(name, bindingFlags, binder, target, providedArgs, modifiers, culture, namedParams)
        End Function

        Protected Overrides Function IsArrayImpl() As Boolean
            Return Me.runtimeType.IsArray
        End Function

        Private Function IsAssignable(ByVal type1 As Type, ByVal type2 As Type) As Boolean
            Dim toType As Type = Me.ResolveTypeFromTypeSystem(type1)
            Dim fromType As Type = Me.ResolveTypeFromTypeSystem(type2)
            Return TypeProvider.IsAssignable(toType, fromType)
        End Function

        Public Overrides Function IsAssignableFrom(ByVal c As Type) As Boolean
            Dim runtimeType As Type = Me.runtimeType
            If (runtimeType.IsGenericTypeDefinition AndAlso Me.IsGenericType) Then
                runtimeType = Me.ResolveGenericTypeFromTypeSystem(runtimeType)
            End If
            Return TypeProvider.IsAssignable(runtimeType, c)
        End Function

        Protected Overrides Function IsByRefImpl() As Boolean
            Return Me.runtimeType.IsByRef
        End Function

        Protected Overrides Function IsCOMObjectImpl() As Boolean
            Return Me.runtimeType.IsCOMObject
        End Function

        Protected Overrides Function IsContextfulImpl() As Boolean
            Return Me.runtimeType.IsContextful
        End Function

        Public Overrides Function IsDefined(ByVal attributeType As Type, ByVal inherit As Boolean) As Boolean
            Return Me.runtimeType.IsDefined(attributeType, inherit)
        End Function

        Protected Overrides Function IsMarshalByRefImpl() As Boolean
            Return Me.runtimeType.IsMarshalByRef
        End Function

        Protected Overrides Function IsPointerImpl() As Boolean
            Return Me.runtimeType.IsPointer
        End Function

        Protected Overrides Function IsPrimitiveImpl() As Boolean
            Return Me.runtimeType.IsPrimitive
        End Function

        Public Overrides Function IsSubclassOf(ByVal potentialBaseType As Type) As Boolean
            Return TypeProvider.IsSubclassOf(Me.runtimeType, potentialBaseType)
        End Function

        Public Overrides Function MakeByRefType() As Type
            Return Me.typeProvider.GetType((Me.FullName & "&"))
        End Function

        Public Overrides Function MakeGenericType(ByVal ParamArray typeArgs As Type()) As Type
            If (typeArgs Is Nothing) Then
                Throw New ArgumentNullException("typeArgs")
            End If
            Dim typeArray As Type() = New Type(typeArgs.Length  - 1) {}
            If Not Me.IsGenericTypeDefinition Then
                Throw New ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "typeArgs")
            End If
            Dim i As Integer
            For i = 0 To typeArgs.Length - 1
                typeArray(i) = typeArgs(i)
                If (typeArray(i) Is Nothing) Then
                    Throw New ArgumentException(SR.GetString("Error_InvalidArgumentValue"), "typeArgs")
                End If
            Next i
            Dim type As Type = [TryCast](Me.boundedTypes.Item(typeArgs),Type)
            If (Not type Is Nothing) Then
                Return type
            End If
            If (((typeArgs.Length = 1) AndAlso (Me.runtimeType Is GetType(Nullable([Of] )))) AndAlso Not typeArgs(0).IsEnum) Then
                Select Case Type.GetTypeCode(typeArgs(0))
                    Case TypeCode.Boolean
                        type = GetType(Nullable([Of] Boolean))
                        goto Label_01E6
                    Case TypeCode.Char
                        type = GetType(Nullable([Of] Char))
                        goto Label_01E6
                    Case TypeCode.[SByte]
                        type = GetType(Nullable([Of] [SByte]))
                        goto Label_01E6
                    Case TypeCode.Byte
                        type = GetType(Nullable([Of] Byte))
                        goto Label_01E6
                    Case TypeCode.Int16
                        type = GetType(Nullable([Of] Short))
                        goto Label_01E6
                    Case TypeCode.UInt16
                        type = GetType(Nullable([Of] UInt16))
                        goto Label_01E6
                    Case TypeCode.Int32
                        type = GetType(Nullable([Of] Integer))
                        goto Label_01E6
                    Case TypeCode.UInt32
                        type = GetType(Nullable([Of] UInt32))
                        goto Label_01E6
                    Case TypeCode.Int64
                        type = GetType(Nullable([Of] Long))
                        goto Label_01E6
                    Case TypeCode.UInt64
                        type = GetType(Nullable([Of] UInt64))
                        goto Label_01E6
                    Case TypeCode.Single
                        type = GetType(Nullable([Of] Single))
                        goto Label_01E6
                    Case TypeCode.Double
                        type = GetType(Nullable([Of] Double))
                        goto Label_01E6
                    Case TypeCode.Decimal
                        type = GetType(Nullable([Of] Decimal))
                        goto Label_01E6
                    Case TypeCode.DateTime
                        type = GetType(Nullable([Of] DateTime))
                        goto Label_01E6
                End Select
                type = New RTTypeWrapper(Me.typeProvider, Me.runtimeType, typeArgs)
            Else
                type = New RTTypeWrapper(Me.typeProvider, Me.runtimeType, typeArgs)
            End If
        Label_01E6:
            Me.boundedTypes.Item(typeArgs) = type
            Return type
        End Function

        Public Overrides Function MakePointerType() As Type
            Return Me.typeProvider.GetType((Me.FullName & "*"))
        End Function

        Friend Sub OnAssemblyRemoved(ByVal removedAssembly As Assembly)
            Dim list As New ArrayList(Me.boundedTypes.Keys)
        Label_0059:
            Dim typeArray As Type()
            For Each typeArray In list
                Dim type As Type
                For Each type In typeArray
                    If (type.Assembly Is removedAssembly) Then
                        Me.boundedTypes.Remove(typeArray)
                        goto Label_0059
                    End If
                Next
            Next
        End Sub

        Friend Function ResolveGenericTypeFromTypeSystem(ByVal type As Type) As Type
            If Me.runtimeType.IsGenericTypeDefinition Then
                Dim declaringType As Type = Nothing
                If Not type.IsNested Then
                    declaringType = Me.typeProvider.GetType((type.Namespace & "." & type.Name))
                Else
                    declaringType = type
                    Dim name As String = type.Name
                    Do While (Not declaringType.DeclaringType Is Nothing)
                        declaringType = declaringType.DeclaringType
                        name = (declaringType.Name & "+" & name)
                    Loop
                    name = (declaringType.Namespace & "." & name)
                    declaringType = Me.typeProvider.GetType(name)
                End If
                If (Not declaringType Is Nothing) Then
                    Return declaringType.MakeGenericType(Me.typeArgs)
                End If
            End If
            Return type
        End Function

        Friend Function ResolveTypeFromTypeSystem(ByVal type As Type) As Type
            If (type Is Nothing) Then
                Return Nothing
            End If
            If type.IsGenericParameter Then
                If (Me.typeArgs Is Nothing) Then
                    Return type
                End If
                type = Me.typeArgs(type.GenericParameterPosition)
            End If
            Dim type2 As Type = Nothing
            Try 
                If Not String.IsNullOrEmpty(type.AssemblyQualifiedName) Then
                    type2 = Me.typeProvider.GetType(type.AssemblyQualifiedName)
                End If
            Catch obj1 As Object
            End Try
            If (type2 Is Nothing) Then
                type2 = type
            End If
            If type2.IsGenericType Then
                type2 = Me.ResolveGenericTypeFromTypeSystem(type2)
            End If
            Return type2
        End Function

        Public Overrides Function ToString() As String
            Return Me.runtimeType.ToString
        End Function


        ' Properties
        Public Overrides ReadOnly Property [Assembly] As Assembly
            Get
                If (Not Me.typeArgs Is Nothing) Then
                    Dim type As Type
                    For Each type In Me.typeArgs
                        If (type.Assembly Is Nothing) Then
                            Return Nothing
                        End If
                    Next
                End If
                Return Me.runtimeType.Assembly
            End Get
        End Property

        Public Overrides ReadOnly Property AssemblyQualifiedName As String
            Get
                Return (Me.FullName & ", " & Me.runtimeType.Assembly.FullName)
            End Get
        End Property

        Public Overrides ReadOnly Property BaseType As Type
            Get
                Return Me.ResolveTypeFromTypeSystem(Me.runtimeType.BaseType)
            End Get
        End Property

        Public Overrides ReadOnly Property ContainsGenericParameters As Boolean
            Get
                If ((Not Me.typeArgs Is Nothing) AndAlso (Me.typeArgs.GetLength(0) > 0)) Then
                    Return False
                End If
                Return Me.runtimeType.ContainsGenericParameters
            End Get
        End Property

        Public Overrides ReadOnly Property DeclaringType As Type
            Get
                If (Me.runtimeType.DeclaringType Is Nothing) Then
                    Return Nothing
                End If
                Return Me.typeProvider.GetType(Me.runtimeType.DeclaringType.AssemblyQualifiedName)
            End Get
        End Property

        Public Overrides ReadOnly Property FullName As String
            Get
                Dim builder As New StringBuilder(Me.runtimeType.FullName)
                If ((Not Me.typeArgs Is Nothing) AndAlso (Me.typeArgs.Length > 0)) Then
                    builder.Append("[")
                    Dim i As Integer
                    For i = 0 To Me.typeArgs.Length - 1
                        builder.Append("[")
                        builder.Append(Me.typeArgs(i).AssemblyQualifiedName)
                        builder.Append("]")
                        If (i < (Me.typeArgs.Length - 1)) Then
                            builder.Append(",")
                        End If
                    Next i
                    builder.Append("]")
                End If
                Return builder.ToString
            End Get
        End Property

        Public Overrides ReadOnly Property GenericParameterPosition As Integer
            Get
                Return Me.runtimeType.GenericParameterPosition
            End Get
        End Property

        Public Overrides ReadOnly Property GUID As Guid
            Get
                Return Me.runtimeType.GUID
            End Get
        End Property

        Public Overrides ReadOnly Property IsGenericParameter As Boolean
            Get
                Return Me.runtimeType.IsGenericParameter
            End Get
        End Property

        Public Overrides ReadOnly Property IsGenericType As Boolean
            Get
                Return (((Not Me.typeArgs Is Nothing) AndAlso (Me.typeArgs.GetLength(0) > 0)) OrElse Me.runtimeType.IsGenericType)
            End Get
        End Property

        Public Overrides ReadOnly Property IsGenericTypeDefinition As Boolean
            Get
                If ((Not Me.typeArgs Is Nothing) AndAlso (Me.typeArgs.GetLength(0) > 0)) Then
                    Return False
                End If
                Return Me.runtimeType.IsGenericTypeDefinition
            End Get
        End Property

        Public Overrides ReadOnly Property MetadataToken As Integer
            Get
                Return Me.runtimeType.MetadataToken
            End Get
        End Property

        Public Overrides ReadOnly Property [Module] As Module
            Get
                Return Me.runtimeType.Module
            End Get
        End Property

        Public Overrides ReadOnly Property Name As String
            Get
                If (Me.IsGenericType AndAlso Not Me.IsGenericTypeDefinition) Then
                    Return Me.GetGenericTypeDefinition.FullName.Substring((Me.Namespace.Length + 1))
                End If
                If (Not Me.Namespace Is Nothing) Then
                    Return Me.FullName.Substring((Me.Namespace.Length + 1))
                End If
                Return Me.FullName
            End Get
        End Property

        Public Overrides ReadOnly Property [Namespace] As String
            Get
                Return Me.runtimeType.Namespace
            End Get
        End Property

        Friend ReadOnly Property Provider As ITypeProvider
            Get
                Return Me.typeProvider
            End Get
        End Property

        Public Overrides ReadOnly Property TypeHandle As RuntimeTypeHandle
            Get
                Return Me.runtimeType.TypeHandle
            End Get
        End Property

        Public Overrides ReadOnly Property UnderlyingSystemType As Type
            Get
                Return Me.runtimeType.UnderlyingSystemType
            End Get
        End Property


        ' Fields
        Private boundedTypes As Hashtable
        Private memberMapping As Hashtable
        Private runtimeType As Type
        Private typeArgs As Type()
        Private typeProvider As ITypeProvider

        ' Nested Types
        Private Class RTConstructorInfoWrapper
            Inherits ConstructorInfo
            ' Methods
            Public Sub New(ByVal rtTypeWrapper As RTTypeWrapper, ByVal ctorInfo As ConstructorInfo)
                Me.rtTypeWrapper = rtTypeWrapper
                Me.ctorInfo = ctorInfo
            End Sub

            Public Overrides Function GetCustomAttributes(ByVal inherit As Boolean) As Object()
                Return Me.ctorInfo.GetCustomAttributes(inherit)
            End Function

            Public Overrides Function GetCustomAttributes(ByVal attributeType As Type, ByVal inherit As Boolean) As Object()
                Return Me.ctorInfo.GetCustomAttributes(attributeType, inherit)
            End Function

            Public Overrides Function GetMethodImplementationFlags() As MethodImplAttributes
                Return Me.ctorInfo.GetMethodImplementationFlags
            End Function

            Public Overrides Function GetParameters() As ParameterInfo()
                If (Me.wrappedParameters Is Nothing) Then
                    Dim list As New List([Of] ParameterInfo)
                    Dim info As ParameterInfo
                    For Each info In Me.ctorInfo.GetParameters
                        list.Add(New RTParameterInfoWrapper(Me.rtTypeWrapper, Me.ctorInfo, info))
                    Next
                    Me.wrappedParameters = list.ToArray
                End If
                Return Me.wrappedParameters
            End Function

            Public Overrides Function Invoke(ByVal invokeAttr As BindingFlags, ByVal binder As Binder, ByVal parameters As Object(), ByVal culture As CultureInfo) As Object
                Return Me.ctorInfo.Invoke(invokeAttr, binder, parameters, culture)
            End Function

            Public Overrides Function Invoke(ByVal obj As Object, ByVal invokeAttr As BindingFlags, ByVal binder As Binder, ByVal parameters As Object(), ByVal culture As CultureInfo) As Object
                Return Me.ctorInfo.Invoke(obj, invokeAttr, binder, parameters, culture)
            End Function

            Public Overrides Function IsDefined(ByVal attributeType As Type, ByVal inherit As Boolean) As Boolean
                Return Me.ctorInfo.IsDefined(attributeType, inherit)
            End Function


            ' Properties
            Public Overrides ReadOnly Property Attributes As MethodAttributes
                Get
                    Return Me.ctorInfo.Attributes
                End Get
            End Property

            Public Overrides ReadOnly Property DeclaringType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.ctorInfo.DeclaringType)
                End Get
            End Property

            Public Overrides ReadOnly Property MemberType As MemberTypes
                Get
                    Return Me.ctorInfo.MemberType
                End Get
            End Property

            Public Overrides ReadOnly Property MethodHandle As RuntimeMethodHandle
                Get
                    Return Me.ctorInfo.MethodHandle
                End Get
            End Property

            Public Overrides ReadOnly Property Name As String
                Get
                    Return Me.ctorInfo.Name
                End Get
            End Property

            Public Overrides ReadOnly Property ReflectedType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.ctorInfo.ReflectedType)
                End Get
            End Property


            ' Fields
            Private ctorInfo As ConstructorInfo
            Private rtTypeWrapper As RTTypeWrapper
            Private wrappedParameters As ParameterInfo()
        End Class

        Private Class RTEventInfoWrapper
            Inherits EventInfo
            ' Methods
            Public Sub New(ByVal rtTypeWrapper As RTTypeWrapper, ByVal eventInfo As EventInfo)
                Me.rtTypeWrapper = rtTypeWrapper
                Me.eventInfo = eventInfo
            End Sub

            Public Overrides Function GetAddMethod(ByVal nonPublic As Boolean) As MethodInfo
                Dim addMethod As MethodInfo = Me.eventInfo.GetAddMethod(nonPublic)
                If (addMethod Is Nothing) Then
                    Return Nothing
                End If
                Return Me.rtTypeWrapper.EnsureMethodWrapped(addMethod)
            End Function

            Public Overrides Function GetCustomAttributes(ByVal inherit As Boolean) As Object()
                Return Me.eventInfo.GetCustomAttributes(inherit)
            End Function

            Public Overrides Function GetCustomAttributes(ByVal attributeType As Type, ByVal inherit As Boolean) As Object()
                Return Me.eventInfo.GetCustomAttributes(attributeType, inherit)
            End Function

            Public Overrides Function GetRaiseMethod(ByVal nonPublic As Boolean) As MethodInfo
                Dim raiseMethod As MethodInfo = Me.eventInfo.GetRaiseMethod(nonPublic)
                If (raiseMethod Is Nothing) Then
                    Return Nothing
                End If
                Return Me.rtTypeWrapper.EnsureMethodWrapped(raiseMethod)
            End Function

            Public Overrides Function GetRemoveMethod(ByVal nonPublic As Boolean) As MethodInfo
                Dim removeMethod As MethodInfo = Me.eventInfo.GetRemoveMethod(nonPublic)
                If (removeMethod Is Nothing) Then
                    Return Nothing
                End If
                Return Me.rtTypeWrapper.EnsureMethodWrapped(removeMethod)
            End Function

            Public Overrides Function IsDefined(ByVal attributeType As Type, ByVal inherit As Boolean) As Boolean
                Return Me.eventInfo.IsDefined(attributeType, inherit)
            End Function


            ' Properties
            Public Overrides ReadOnly Property Attributes As EventAttributes
                Get
                    Return Me.eventInfo.Attributes
                End Get
            End Property

            Public Overrides ReadOnly Property DeclaringType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.eventInfo.DeclaringType)
                End Get
            End Property

            Public Overrides ReadOnly Property MemberType As MemberTypes
                Get
                    Return Me.eventInfo.MemberType
                End Get
            End Property

            Public Overrides ReadOnly Property MetadataToken As Integer
                Get
                    Return Me.eventInfo.MetadataToken
                End Get
            End Property

            Public Overrides ReadOnly Property [Module] As Module
                Get
                    Return Me.eventInfo.Module
                End Get
            End Property

            Public Overrides ReadOnly Property Name As String
                Get
                    Return Me.eventInfo.Name
                End Get
            End Property

            Public Overrides ReadOnly Property ReflectedType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.eventInfo.ReflectedType)
                End Get
            End Property


            ' Fields
            Private eventInfo As EventInfo
            Private rtTypeWrapper As RTTypeWrapper
        End Class

        Private Class RTFieldInfoWrapper
            Inherits FieldInfo
            ' Methods
            Public Sub New(ByVal rtTypeWrapper As RTTypeWrapper, ByVal fieldInfo As FieldInfo)
                Me.rtTypeWrapper = rtTypeWrapper
                Me.fieldInfo = fieldInfo
            End Sub

            Public Overrides Function GetCustomAttributes(ByVal inherit As Boolean) As Object()
                Return Me.fieldInfo.GetCustomAttributes(inherit)
            End Function

            Public Overrides Function GetCustomAttributes(ByVal attributeType As Type, ByVal inherit As Boolean) As Object()
                Return Me.fieldInfo.GetCustomAttributes(attributeType, inherit)
            End Function

            Public Overrides Function GetValue(ByVal obj As Object) As Object
                Return Me.fieldInfo.GetValue(obj)
            End Function

            Public Overrides Function IsDefined(ByVal attributeType As Type, ByVal inherit As Boolean) As Boolean
                Return Me.fieldInfo.IsDefined(attributeType, inherit)
            End Function

            Public Overrides Sub SetValue(ByVal obj As Object, ByVal value As Object, ByVal invokeAttr As BindingFlags, ByVal binder As Binder, ByVal culture As CultureInfo)
                Me.fieldInfo.SetValue(obj, value, invokeAttr, binder, culture)
            End Sub


            ' Properties
            Public Overrides ReadOnly Property Attributes As FieldAttributes
                Get
                    Return Me.fieldInfo.Attributes
                End Get
            End Property

            Public Overrides ReadOnly Property DeclaringType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.fieldInfo.DeclaringType)
                End Get
            End Property

            Public Overrides ReadOnly Property FieldHandle As RuntimeFieldHandle
                Get
                    Return Me.fieldInfo.FieldHandle
                End Get
            End Property

            Public Overrides ReadOnly Property FieldType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.fieldInfo.FieldType)
                End Get
            End Property

            Public Overrides ReadOnly Property MemberType As MemberTypes
                Get
                    Return Me.fieldInfo.MemberType
                End Get
            End Property

            Public Overrides ReadOnly Property MetadataToken As Integer
                Get
                    Return Me.fieldInfo.MetadataToken
                End Get
            End Property

            Public Overrides ReadOnly Property [Module] As Module
                Get
                    Return Me.fieldInfo.Module
                End Get
            End Property

            Public Overrides ReadOnly Property Name As String
                Get
                    Return Me.fieldInfo.Name
                End Get
            End Property

            Public Overrides ReadOnly Property ReflectedType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.fieldInfo.ReflectedType)
                End Get
            End Property


            ' Fields
            Private fieldInfo As FieldInfo
            Private rtTypeWrapper As RTTypeWrapper
        End Class

        Private Class RTMethodInfoWrapper
            Inherits MethodInfo
            ' Methods
            Public Sub New(ByVal rtTypeWrapper As RTTypeWrapper, ByVal methodInfo As MethodInfo)
                Me.rtTypeWrapper = rtTypeWrapper
                Me.methodInfo = methodInfo
            End Sub

            Public Overrides Function GetBaseDefinition() As MethodInfo
                Return Me.methodInfo.GetBaseDefinition
            End Function

            Public Overrides Function GetCustomAttributes(ByVal inherit As Boolean) As Object()
                Return Me.methodInfo.GetCustomAttributes(inherit)
            End Function

            Public Overrides Function GetCustomAttributes(ByVal attributeType As Type, ByVal inherit As Boolean) As Object()
                Return Me.methodInfo.GetCustomAttributes(attributeType, inherit)
            End Function

            Public Overrides Function GetMethodBody() As MethodBody
                Return Me.methodInfo.GetMethodBody
            End Function

            Public Overrides Function GetMethodImplementationFlags() As MethodImplAttributes
                Return Me.methodInfo.GetMethodImplementationFlags
            End Function

            Public Overrides Function GetParameters() As ParameterInfo()
                If (Me.wrappedParameters Is Nothing) Then
                    Dim list As New List([Of] ParameterInfo)
                    Dim info As ParameterInfo
                    For Each info In Me.methodInfo.GetParameters
                        list.Add(New RTParameterInfoWrapper(Me.rtTypeWrapper, Me.methodInfo, info))
                    Next
                    Me.wrappedParameters = list.ToArray
                End If
                Return Me.wrappedParameters
            End Function

            Public Overrides Function Invoke(ByVal obj As Object, ByVal invokeAttr As BindingFlags, ByVal binder As Binder, ByVal parameters As Object(), ByVal culture As CultureInfo) As Object
                Return Me.methodInfo.Invoke(obj, invokeAttr, binder, parameters, culture)
            End Function

            Public Overrides Function IsDefined(ByVal attributeType As Type, ByVal inherit As Boolean) As Boolean
                Return Me.methodInfo.IsDefined(attributeType, inherit)
            End Function


            ' Properties
            Public Overrides ReadOnly Property Attributes As MethodAttributes
                Get
                    Return Me.methodInfo.Attributes
                End Get
            End Property

            Public Overrides ReadOnly Property CallingConvention As CallingConventions
                Get
                    Return Me.methodInfo.CallingConvention
                End Get
            End Property

            Public Overrides ReadOnly Property DeclaringType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.methodInfo.DeclaringType)
                End Get
            End Property

            Public Overrides ReadOnly Property MemberType As MemberTypes
                Get
                    Return Me.methodInfo.MemberType
                End Get
            End Property

            Public Overrides ReadOnly Property MetadataToken As Integer
                Get
                    Return Me.methodInfo.MetadataToken
                End Get
            End Property

            Public Overrides ReadOnly Property MethodHandle As RuntimeMethodHandle
                Get
                    Return Me.methodInfo.MethodHandle
                End Get
            End Property

            Public Overrides ReadOnly Property [Module] As Module
                Get
                    Return Me.methodInfo.Module
                End Get
            End Property

            Public Overrides ReadOnly Property Name As String
                Get
                    Return Me.methodInfo.Name
                End Get
            End Property

            Public Overrides ReadOnly Property ReflectedType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.methodInfo.ReflectedType)
                End Get
            End Property

            Public Overrides ReadOnly Property ReturnParameter As ParameterInfo
                Get
                    Return New RTParameterInfoWrapper(Me.rtTypeWrapper, Me, Me.methodInfo.ReturnParameter)
                End Get
            End Property

            Public Overrides ReadOnly Property ReturnType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.methodInfo.ReturnType)
                End Get
            End Property

            Public Overrides ReadOnly Property ReturnTypeCustomAttributes As ICustomAttributeProvider
                Get
                    Return Me.methodInfo.ReturnTypeCustomAttributes
                End Get
            End Property


            ' Fields
            Private methodInfo As MethodInfo
            Private rtTypeWrapper As RTTypeWrapper
            Private wrappedParameters As ParameterInfo()
        End Class

        Private Class RTParameterInfoWrapper
            Inherits ParameterInfo
            ' Methods
            Public Sub New(ByVal rtTypeWrapper As RTTypeWrapper, ByVal parentMember As MemberInfo, ByVal paramInfo As ParameterInfo)
                Me.parentMember = parentMember
                Me.rtTypeWrapper = rtTypeWrapper
                Me.paramInfo = paramInfo
            End Sub

            Public Overrides Function GetCustomAttributes(ByVal inherit As Boolean) As Object()
                Return Me.paramInfo.GetCustomAttributes(inherit)
            End Function

            Public Overrides Function GetOptionalCustomModifiers() As Type()
                Return Me.paramInfo.GetOptionalCustomModifiers
            End Function

            Public Overrides Function GetRequiredCustomModifiers() As Type()
                Return Me.paramInfo.GetRequiredCustomModifiers
            End Function

            Public Overrides Function IsDefined(ByVal attributeType As Type, ByVal inherit As Boolean) As Boolean
                Return Me.paramInfo.IsDefined(attributeType, inherit)
            End Function


            ' Properties
            Public Overrides ReadOnly Property Attributes As ParameterAttributes
                Get
                    Return Me.paramInfo.Attributes
                End Get
            End Property

            Public Overrides ReadOnly Property DefaultValue As Object
                Get
                    Throw New NotImplementedException
                End Get
            End Property

            Public Overrides ReadOnly Property Member As MemberInfo
                Get
                    Return Me.parentMember
                End Get
            End Property

            Public Overrides ReadOnly Property Name As String
                Get
                    Return Me.paramInfo.Name
                End Get
            End Property

            Public Overrides ReadOnly Property ParameterType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.paramInfo.ParameterType)
                End Get
            End Property

            Public Overrides ReadOnly Property Position As Integer
                Get
                    Return Me.paramInfo.Position
                End Get
            End Property


            ' Fields
            Private paramInfo As ParameterInfo
            Private parentMember As MemberInfo
            Private rtTypeWrapper As RTTypeWrapper
        End Class

        Private Class RTPropertyInfoWrapper
            Inherits PropertyInfo
            ' Methods
            Public Sub New(ByVal rtTypeWrapper As RTTypeWrapper, ByVal propertyInfo As PropertyInfo)
                Me.rtTypeWrapper = rtTypeWrapper
                Me.propertyInfo = propertyInfo
            End Sub

            Public Overrides Function GetAccessors(ByVal nonPublic As Boolean) As MethodInfo()
                Dim list As New List([Of] MethodInfo)
                Dim info As MethodInfo
                For Each info In Me.propertyInfo.GetAccessors(nonPublic)
                    list.Add(Me.rtTypeWrapper.EnsureMethodWrapped(info))
                Next
                Return list.ToArray
            End Function

            Public Overrides Function GetCustomAttributes(ByVal inherit As Boolean) As Object()
                Return Me.propertyInfo.GetCustomAttributes(inherit)
            End Function

            Public Overrides Function GetCustomAttributes(ByVal attributeType As Type, ByVal inherit As Boolean) As Object()
                Return Me.propertyInfo.GetCustomAttributes(attributeType, inherit)
            End Function

            Public Overrides Function GetGetMethod(ByVal nonPublic As Boolean) As MethodInfo
                Dim getMethod As MethodInfo = Me.propertyInfo.GetGetMethod(nonPublic)
                If (getMethod Is Nothing) Then
                    Return Nothing
                End If
                Return Me.rtTypeWrapper.EnsureMethodWrapped(getMethod)
            End Function

            Public Overrides Function GetIndexParameters() As ParameterInfo()
                If (Me.wrappedParameters Is Nothing) Then
                    Dim list As New List([Of] ParameterInfo)
                    Dim info As ParameterInfo
                    For Each info In Me.propertyInfo.GetIndexParameters
                        list.Add(New RTParameterInfoWrapper(Me.rtTypeWrapper, Me.propertyInfo, info))
                    Next
                    Me.wrappedParameters = list.ToArray
                End If
                Return Me.wrappedParameters
            End Function

            Public Overrides Function GetSetMethod(ByVal nonPublic As Boolean) As MethodInfo
                Dim setMethod As MethodInfo = Me.propertyInfo.GetSetMethod(nonPublic)
                If (setMethod Is Nothing) Then
                    Return Nothing
                End If
                Return Me.rtTypeWrapper.EnsureMethodWrapped(setMethod)
            End Function

            Public Overrides Function GetValue(ByVal obj As Object, ByVal invokeAttr As BindingFlags, ByVal binder As Binder, ByVal index As Object(), ByVal culture As CultureInfo) As Object
                Return Me.propertyInfo.GetValue(obj, invokeAttr, binder, index, culture)
            End Function

            Public Overrides Function IsDefined(ByVal attributeType As Type, ByVal inherit As Boolean) As Boolean
                Return Me.propertyInfo.IsDefined(attributeType, inherit)
            End Function

            Public Overrides Sub SetValue(ByVal obj As Object, ByVal value As Object, ByVal invokeAttr As BindingFlags, ByVal binder As Binder, ByVal index As Object(), ByVal culture As CultureInfo)
                Me.propertyInfo.SetValue(obj, value, invokeAttr, binder, index, culture)
            End Sub


            ' Properties
            Public Overrides ReadOnly Property Attributes As PropertyAttributes
                Get
                    Return Me.propertyInfo.Attributes
                End Get
            End Property

            Public Overrides ReadOnly Property CanRead As Boolean
                Get
                    Return Me.propertyInfo.CanRead
                End Get
            End Property

            Public Overrides ReadOnly Property CanWrite As Boolean
                Get
                    Return Me.propertyInfo.CanWrite
                End Get
            End Property

            Public Overrides ReadOnly Property DeclaringType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.propertyInfo.DeclaringType)
                End Get
            End Property

            Public Overrides ReadOnly Property MemberType As MemberTypes
                Get
                    Return Me.propertyInfo.MemberType
                End Get
            End Property

            Public Overrides ReadOnly Property MetadataToken As Integer
                Get
                    Return Me.propertyInfo.MetadataToken
                End Get
            End Property

            Public Overrides ReadOnly Property [Module] As Module
                Get
                    Return Me.propertyInfo.Module
                End Get
            End Property

            Public Overrides ReadOnly Property Name As String
                Get
                    Return Me.propertyInfo.Name
                End Get
            End Property

            Public Overrides ReadOnly Property PropertyType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.propertyInfo.PropertyType)
                End Get
            End Property

            Public Overrides ReadOnly Property ReflectedType As Type
                Get
                    Return Me.rtTypeWrapper.ResolveTypeFromTypeSystem(Me.propertyInfo.ReflectedType)
                End Get
            End Property


            ' Fields
            Private propertyInfo As PropertyInfo
            Private rtTypeWrapper As RTTypeWrapper
            Private wrappedParameters As ParameterInfo()
        End Class

        Private Class TypeArrayComparer
            Implements IEqualityComparer
            ' Methods
            Private Function System.Collections.IEqualityComparer.Equals(ByVal x As Object, ByVal y As Object) As Boolean Implements IEqualityComparer.Equals
                Dim array As Array = [TryCast](x,Array)
                Dim array2 As Array = [TryCast](y,Array)
                If (((array Is Nothing) OrElse (array2 Is Nothing)) OrElse ((array.Rank <> 1) OrElse (array2.Rank <> 1))) Then
                    Return False
                End If
                Dim flag As Boolean = False
                If (array.Length = array2.Length) Then
                    Dim i As Integer = 0
                    Do While (Not flag AndAlso (i < array.Length))
                        flag = (Not array.GetValue(i) Is array2.GetValue(i))
                        i += 1
                    Loop
                Else
                    flag = True
                End If
                Return Not flag
            End Function

            Private Function System.Collections.IEqualityComparer.GetHashCode(ByVal obj As Object) As Integer Implements IEqualityComparer.GetHashCode
                Return 0
            End Function

        End Class
    End Class
End Namespace

